package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.bean.MenuType;
import com.wm.bean.ResMenu;
import com.wm.bean.Restaurant;
import com.wm.comm.Constant;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.MenuTypeService;
import com.wm.service.ResMenuService;
import com.wm.service.RestaurantService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.ui.OptionsInt;
import com.wm.web.ui.OptionsLong;

public class AdminRestaurantMenu extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminRestaurantMenu.class);
	
	private ResMenuService resMenuService;
	private MenuTypeService menuTypeService;
	private RestaurantService restaurantService;

	public ResMenuService getResMenuService() {
		return resMenuService;
	}

	public void setResMenuService(ResMenuService resMenuService) {
		this.resMenuService = resMenuService;
	}
	
	public MenuTypeService getMenuTypeService() {
		return menuTypeService;
	}

	public void setMenuTypeService(MenuTypeService menuTypeService) {
		this.menuTypeService = menuTypeService;
	}
	
	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}


	private List isShowList = new ArrayList();
	private List typeList = new ArrayList();
	private List isPromotionList = new ArrayList();
	private List isFaciaList = new ArrayList();

	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}
	
	public List getTypeList() {
		return typeList;
	}

	public void setTypeList(List typeList) {
		this.typeList = typeList;
	}
	
	public List getIsPromotionList() {
		return isPromotionList;
	}

	public void setIsPromotionList(List isPromotionList) {
		this.isPromotionList = isPromotionList;
	}
	
	public List getIsFaciaList() {
		return isFaciaList;
	}

	public void setIsFaciaList(List isFaciaList) {
		this.isFaciaList = isFaciaList;
	}

	private void setIsShowListValues(){
		isShowList = Constant.IS_SHOW;
	}
	
	private void setTypeListValues(){
		typeList.add(new OptionsLong(-1, "请选择"));
		List l = this.getMenuTypeService().findMenuTypeByIsShow(this.isShow, "id", 0);
		for(int i=0;i<l.size();i++){
			MenuType mt = (MenuType) l.get(i);
			typeList.add(new OptionsLong(mt.getId(), mt.getTypeName()));
		}
	}
	
	private void setIsPromotionListValues(){
		isPromotionList.add(new OptionsLong(-1, "请选择"));
		isPromotionList.add(new OptionsLong(0, "不促销"));
		isPromotionList.add(new OptionsLong(1, "促销"));
	}
	
	private void setIsFaciaListValues(){
		isFaciaList.add(new OptionsLong(-1, "请选择"));
		isFaciaList.add(new OptionsLong(0, "非招牌"));
		isFaciaList.add(new OptionsLong(1, "招牌"));
	}


	private void initValues(){
		setIsShowListValues();
		setTypeListValues();
		setIsPromotionListValues();
		setIsFaciaListValues();
	}
	
	private void listInitValues(){
		setIsShowListValues();
		setTypeListValues();
		setIsPromotionListValues();
		setIsFaciaListValues();
	}


	private long id;					//主键ID
	private long resId;			    	//饭店餐馆表的ID
	private String menuName;			//菜名称
	private String menuInitials;		//菜名首字母
	private String menuAnglic;			//菜名的简拼
	private String menuPhonetic;		//菜名的全拼
	private int price;					//价格
	private int discount;				//折扣价
	private int isPromotion = -1;		//是否促销0不促销1促销
	private long typeId = -1;			//菜单类别表ID
	private int isFacia = -1;			//是否招牌0不是1是
	private int isShow = -1;			//是否显示
	private int orderNum;				//排序字段
	private String resName;				//饭店名称
	private String menuMemo;			//备注
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getResId() {
		return resId;
	}

	public void setResId(long resId) {
		this.resId = resId;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public String getMenuInitials() {
		return menuInitials;
	}

	public void setMenuInitials(String menuInitials) {
		this.menuInitials = menuInitials;
	}

	public String getMenuAnglic() {
		return menuAnglic;
	}

	public void setMenuAnglic(String menuAnglic) {
		this.menuAnglic = menuAnglic;
	}

	public String getMenuPhonetic() {
		return menuPhonetic;
	}

	public void setMenuPhonetic(String menuPhonetic) {
		this.menuPhonetic = menuPhonetic;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getDiscount() {
		return discount;
	}

	public void setDiscount(int discount) {
		this.discount = discount;
	}

	public int getIsPromotion() {
		return isPromotion;
	}

	public void setIsPromotion(int isPromotion) {
		this.isPromotion = isPromotion;
	}

	public long getTypeId() {
		return typeId;
	}

	public void setTypeId(long typeId) {
		this.typeId = typeId;
	}

	public int getIsFacia() {
		return isFacia;
	}

	public void setIsFacia(int isFacia) {
		this.isFacia = isFacia;
	}

	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public int getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}

	public String getResName() {
		return resName;
	}

	public void setResName(String resName) {
		this.resName = resName;
	}	

	public String getMenuMemo() {
		return menuMemo;
	}

	public void setMenuMemo(String menuMemo) {
		this.menuMemo = menuMemo;
	}
	
	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_restaurant_menu.action?menuName=" + this.menuName + "&typeId="+ this.typeId +"&isShow=" + this.isShow);
		this.setPageList(this.getResMenuService().findResMenus(-1,this.menuName, this.typeId,-1,this.isShow,"id", 1, pages));
		return SUCCESS;
	}
	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加
		ResMenu resMenu = new ResMenu();
		resMenu.setResId(this.resId);
		resMenu.setMenuName(this.menuName);
		resMenu.setMenuInitials(this.menuInitials);
		resMenu.setMenuAnglic(this.menuAnglic);
		resMenu.setMenuPhonetic(this.menuPhonetic);
		resMenu.setMenuInitials(this.menuInitials);
		resMenu.setPrice(this.price);
		resMenu.setDiscount(this.discount);
		resMenu.setIsPromotion(this.isPromotion);
		resMenu.setTypeId(this.typeId);
		resMenu.setBuyNum(0);
		resMenu.setScore(0);
		resMenu.setScorePeople(0);
		resMenu.setIsFacia(this.isFacia);
		resMenu.setIsShow(this.isShow);
		resMenu.setOrderNum(this.orderNum);
		resMenu.setOrderTime(new Date());
		resMenu.setAddTime(new Date());
		resMenu.setMenuMemo(this.menuMemo);
		try {
			this.getResMenuService().saveResMenu(resMenu);
		} catch (Exception e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		ResMenu resMenu = this.getResMenuService().findResMenuById(this.id);
		this.setResId(resMenu.getResId());
		this.setMenuName(resMenu.getMenuName());
		this.setMenuInitials(resMenu.getMenuInitials());
		this.setMenuAnglic(resMenu.getMenuAnglic());
		this.setMenuPhonetic(resMenu.getMenuPhonetic());
		this.setMenuInitials(resMenu.getMenuInitials());
		this.setPrice(resMenu.getPrice());
		this.setDiscount(resMenu.getDiscount());
		this.setIsPromotion(resMenu.getIsPromotion());
		this.setTypeId(resMenu.getTypeId());
		this.setIsFacia(resMenu.getIsFacia());
		this.setIsShow(resMenu.getIsShow());
		this.setOrderNum(resMenu.getOrderNum());
		this.setMenuMemo(resMenu.getMenuMemo());
		Restaurant restaurant = this.getRestaurantService().findRestaurantById(resMenu.getResId());
		this.setResName(restaurant.getResName());
		return INPUT;
	}
	
	public String doupdate(){
		//添加
		ResMenu resMenu = this.getResMenuService().findResMenuById(this.id);
		resMenu.setResId(this.resId);
		resMenu.setMenuName(this.menuName);
		resMenu.setMenuInitials(this.menuInitials);
		resMenu.setMenuAnglic(this.menuAnglic);
		resMenu.setMenuPhonetic(this.menuPhonetic);
		resMenu.setMenuInitials(this.menuInitials);
		resMenu.setPrice(this.price);
		resMenu.setDiscount(this.discount);
		resMenu.setIsPromotion(this.isPromotion);
		resMenu.setTypeId(this.typeId);
		resMenu.setBuyNum(0);
		resMenu.setScore(0);
		resMenu.setScorePeople(0);
		resMenu.setIsFacia(this.isFacia);
		resMenu.setIsShow(this.isShow);
		resMenu.setOrderNum(this.orderNum);
		resMenu.setOrderTime(new Date());
		resMenu.setAddTime(new Date());
		resMenu.setMenuMemo(this.menuMemo);
		try {
			this.getResMenuService().updateResMenu(resMenu);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String orders(){
		ResMenu resMenu = this.getResMenuService().findResMenuById(this.id);
		resMenu.setOrderTime(new Date());
		try {
			this.getResMenuService().updateResMenu(resMenu);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getResMenuService().removeResMenuById(this.id);
		return "index";
	}


	
	
}
